package com.nanjia.boot.delivery.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.core.security.util.SecurityUtils;
import com.nanjia.boot.invoicetypeconfig.model.entity.InvoiceType;
import com.nanjia.boot.invoicetypeconfig.model.query.InvoiceTypeQuery;
import com.nanjia.boot.invoicetypeconfig.service.InvoiceTypeService;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.delivery.model.form.InvoiceForm;
import com.nanjia.boot.delivery.model.query.InvoiceQuery;

import com.nanjia.boot.delivery.model.vo.InvoiceVO;

import com.nanjia.boot.delivery.service.InvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发票管理前端控制层
 *
 * @author 周洋
 * @since 2025-06-13 10:55
 */
@Tag(name = "发票管理接口")
@RestController
@RequestMapping("/api/v1/invoice")
@RequiredArgsConstructor
public class InvoiceController  {

    private final InvoiceService invoiceService;



    private final InvoiceTypeService invoiceTypeService;
    @Operation(summary = "发票管理分页列表")
    @GetMapping("/page")
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:query')")
    public PageResult<InvoiceVO> getInvoicePage(InvoiceQuery queryParams ) {
        queryParams.setSubdomainUserCode(SecurityUtils.getUserCode());
        queryParams.setSubdomainCode(SecurityUtils.getSubdomainCode());
        IPage<InvoiceVO> result = invoiceService.getInvoicePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增发票管理")
    @PostMapping
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:add')")
    public Result<Void> saveInvoice(@RequestBody @Valid InvoiceForm formData , HttpServletRequest request) {
        boolean result = invoiceService.saveInvoice(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取发票管理表单数据")
    @GetMapping("/{id}/form")
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:edit')")
    public Result<InvoiceForm> getInvoiceForm(
        @Parameter(description = "发票管理ID") @PathVariable Long id
    ) {
        InvoiceForm formData = invoiceService.getInvoiceFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改发票管理")
    @PutMapping(value = "/{id}")
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:edit')")
    public Result<Void> updateInvoice(
            @Parameter(description = "发票管理ID") @PathVariable Long id,
            @RequestBody @Validated InvoiceForm formData
    ) {
        boolean result = invoiceService.updateInvoice(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除发票管理")
    @DeleteMapping("/{ids}")
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:delete')")
    public Result<Void> deleteInvoices(
        @Parameter(description = "发票管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = invoiceService.deleteInvoices(ids);
        return Result.judge(result);
    }



    @Operation(summary = "发票机构下拉框")
    @GetMapping("/getInvoiceTypeList")
    // @PreAuthorize("@ss.hasPerm('invoice:invoice:query')")
    public Result<List<InvoiceType>> getInvoiceTypeList(InvoiceTypeQuery queryParams) {
        List<InvoiceType> result = invoiceTypeService.getInvoiceTypeList(queryParams.getInvoiceTypeStatus());
        return Result.success(result);
    }

}
